package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.Code;
import com.qian.api.ddj.service.ICodeService;
import com.qian.service.ddj.dao.CodeRepository;
import com.qian.service.ddj.entity.DdjCode;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CodeServiceImpl extends BaseServiceImpl<Code,DdjCode,Integer> implements ICodeService {
    @Autowired
    private CodeRepository dao;
    @Override
    public Code save(Code code) {
        if (code.isNew()){
            code = entity2model(dao.save(model2Entity(code)));
        }else{
            DdjCode ddjCode = dao.findOne(code.getId());
            if (isNotEmpty(code.getCode())){
                ddjCode.setCode(code.getCode());
            }
           code = entity2model( dao.save(ddjCode));
        }
        return code;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjCode model2Entity(Code model) {
        DdjCode ddjCode = null;
        if (isNotEmpty(model)){
            ddjCode = new DdjCode();
            ddjCode.setId(model.getId());
            ddjCode.setCode(model.getCode());
        }
        return ddjCode;
    }

    @Override
    protected Code entity2model(DdjCode entity) {
        Code code = null;
        if (isNotEmpty(entity)){
            code = new Code();
            code.setCode(entity.getCode());
            code.setId(entity.getId());
        }
        return code;
    }
}
